//
//  FindViewController.m
//  庐山通
//
//  Created by zcj on 15-10-23.
//  Copyright (c) 2015年 郑朝军. All rights reserved.
//

#import "FindViewController.h"
#import "MJRefresh.h"
#import "Grouble.h"
#import "HGWaterFlow.h"
#import "MyCollection.h"
#import "FindModelFrame.h"
#import "SelectFindHttp.h"
#import "MJExtension.h"
#import "FindModel.h"
#import "GoodsMainImageModel.h"


#define MyFindCell @"HGFindCellName"

@interface FindViewController ()<MJRefreshBaseViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,HGWaterFlowDelegate>
@property (nonatomic,weak)UICollectionView * findCollectionView;

@property (nonatomic,strong) MJRefreshHeaderView *header;
@property (nonatomic,strong) MJRefreshFooterView *footer;

@property (nonatomic,strong)NSMutableArray *dataFrame;
@property (nonatomic,strong)NSMutableArray *tempArray;

@end

@implementation FindViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (NSMutableArray *)tempArray
{
    if (_tempArray == nil) {
        _tempArray = [[NSMutableArray alloc]init];
    }
    return _tempArray;
}
- (NSMutableArray *)dataFrame
{
    if (_dataFrame == nil) {
        _dataFrame = [NSMutableArray arrayWithCapacity:100];
    }
    return _dataFrame;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self addCollection];
    [self setRush];
    //self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
}
- (void)addCollection
{
    // collection的一个布局
    HGWaterFlow * flow = [[HGWaterFlow alloc]init];
    // 拿到每一个格子的size
    flow.delegate = self;
    flow.columsCount = 2;
    flow.sectionInset = UIEdgeInsetsMake(0, 10, 40, 10);
    
    
    UICollectionView * FindCollection = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flow];
    
    FindCollection.backgroundColor = ZCJColor(237, 237, 237);

    [FindCollection registerClass:[MyCollection class] forCellWithReuseIdentifier:MyFindCell];
    
    FindCollection.delegate = self;
    FindCollection.dataSource = self;
    [self.view addSubview:FindCollection];
    self.findCollectionView = FindCollection;
    
}
- (void)setRush
{
    MJRefreshHeaderView * head = [[MJRefreshHeaderView alloc]init];
    head.scrollView = self.findCollectionView;
    head.delegate = self;
    [head beginRefreshing];
    self.header = head;
    MJRefreshFooterView * foot = [[MJRefreshFooterView alloc]init];
    foot.scrollView = self.findCollectionView;
    foot.delegate = self;
    self.footer = foot;
}
- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    if ([refreshView isKindOfClass:[MJRefreshFooterView class]]) { // 上拉刷新
        [self loadMoreData];
    } else { // 下拉刷新
        [self loadNewData];
    }
}
- (void)loadMoreData
{
    [SelectFindHttp getWirhUrl:HGURL parms:nil success:^(id json) {
        NSMutableArray *bannerFrame=[NSMutableArray array];
        
        NSArray *tempGoods = json[@"data"][@"goods_list"];
        NSLog(@"%@",tempGoods);
        
        // 获取模型对象
        NSArray *findArrayTemp = [FindModel objectArrayWithKeyValuesArray:tempGoods];
        
        for(FindModel * findModel in findArrayTemp){
            FindModelFrame * findModelFrame = [[FindModelFrame alloc]init];
            findModelFrame.findModel = findModel;
            [bannerFrame addObject:findModelFrame];
        }
        // 把新获取的frame放入temparray里面去
        //self.dataFrame = [bannerFrame addObject:self.dataFrame];
        //self.dataFrame = [bannerFrame addObject:self.dataFrame];
        self.dataFrame = bannerFrame;
        //        NSMutableArray * tempArray = [NSMutableArray array];
        //        [tempArray addObject:bannerFrame];
        //        [tempArray addObject:self.dataFrame];
        //        self.dataFrame = tempArray;
        [self.findCollectionView reloadData];
        [self.footer endRefreshing];
        
        [MBProgressHUD showSuccess:@"下拉刷新成功"];
    } failture:^(id error) {
        NSLog(@"%@",error);
    }];
}
/***
 1:请求数据成nsarray
 2:转换成模型数据
 3:算出数据的frame
 4:放入临时数组中
 5:把临时数组中的数据负值给数据
 ***/
- (void)loadNewData
{
    [SelectFindHttp getWirhUrl:HGURL parms:nil success:^(id json) {
        NSMutableArray *bannerFrame=[NSMutableArray array];
        
        NSArray *tempGoods = json[@"data"][@"goods_list"];
        NSLog(@"%@",tempGoods);
        
        // 获取模型对象
        NSArray *findArrayTemp = [FindModel objectArrayWithKeyValuesArray:tempGoods];
        
        for(FindModel * findModel in findArrayTemp){
            FindModelFrame * findModelFrame = [[FindModelFrame alloc]init];
            findModelFrame.findModel = findModel;
            [bannerFrame addObject:findModelFrame];
        }
        // 把新获取的frame放入temparray里面去
        //self.dataFrame = [bannerFrame addObject:self.dataFrame];
        //self.dataFrame = [bannerFrame addObject:self.dataFrame];
        self.dataFrame = bannerFrame;
//        NSMutableArray * tempArray = [NSMutableArray array];
//        [tempArray addObject:bannerFrame];
//        [tempArray addObject:self.dataFrame];
//        self.dataFrame = tempArray;
        [self.findCollectionView reloadData];
        [self.header endRefreshing];
        [MBProgressHUD showSuccess:@"上拉刷新成功"];
    } failture:^(id error) {
        NSLog(@"%@",error);
    }];
}
- (void)dealloc
{
    [self.footer free];
    [self.header free];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataFrame.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyCollection *cell = [MyCollection MyCollection:collectionView cellForItemAtIndexPath:indexPath];
   
    FindModelFrame * modelFrame = self.dataFrame[indexPath.item];
    
    NSString * imgUrl = modelFrame.findModel.main_image.image_original;
    
    //ZCJLog(@"%@",imgUrl);
    
    
    cell.findModelFrame = modelFrame;
    
    return cell;
}
#pragma mark 返回瀑布流的高度

- (CGFloat)waterFlow:(HGWaterFlow *)waterFlow heightForWidth:(CGFloat)width indexPath:(NSIndexPath *)indexPath
{
//    NSMutableArray * tempArray = self.dataFrame[indexPath.row];
    FindModelFrame * modelFrame = self.dataFrame[indexPath.row];
    
    return modelFrame.cellH;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
